/*
 * Copyright 2021 yhuan416
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "sbull.h"

#include "hdf_log.h"
#include "hdf_base.h"
#include "hdf_device_desc.h"
#include "device_resource_if.h"

#include "sbull_internal.h"

int hdf_sbull_driver_init(struct HdfDeviceObject *deviceObject)
{
    int ret = HDF_FAILURE;
    struct DeviceResourceIface *p = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
    struct Sbull *sbull = NULL;
    uint32_t vbase;
    uint32_t size;

    if (deviceObject == NULL ||  deviceObject->property == NULL) {
        dprintf("[%s]deviceObject or property is null", __func__);
        return HDF_ERR_INVALID_PARAM;
    }

    // 获取flash在内存中的地址
    if ((ret = p->GetUint32(deviceObject->property, "vbase", &vbase, 0))) {
        dprintf("[%s]GetUint32 error:%d", __func__, ret);
        return HDF_FAILURE;
    }

    if ((ret = p->GetUint32(deviceObject->property, "size", &size, 0))) {
        dprintf("[%s]GetUint32 error:%d", __func__, ret);
        return HDF_FAILURE;
    }

    // 创建sbull
    sbull = (struct Sbull *)LOS_MemAlloc(m_aucSysMem0, sizeof(struct Sbull));
    if (sbull == NULL) {
        return HDF_FAILURE;
    }

    // init sbull
    ret = sbull_init(sbull, vbase, size);
    if (ret != 0) {
        LOS_MemFree(m_aucSysMem0, sbull);
        sbull = NULL;
        dprintf("sbullInit fail.\r\n");
        return HDF_FAILURE;
    }

    dprintf("sbullInit ok.\r\n");
    return ret;
}

struct HdfDriverEntry g_sbull_driver_entry = {
    .moduleVersion = 1,
    .moduleName = "sbull",
    .Bind = NULL,
    .Init = hdf_sbull_driver_init,
    .Release = NULL,
};
HDF_INIT(g_sbull_driver_entry);
